package com.sykj.msg;



import com.sykj.bean.bis.BisBagRecord;
import com.sykj.bean.bis.BisBusinessOrder;
import com.sykj.bean.bis.BisNurseBag;
import com.sykj.bean.card.CardUserHit;
import com.sykj.service.BisBagRecordService;
import com.sykj.service.BisBusinessOrderService;
import com.sykj.service.BisNurseBagService;
import com.sykj.service.CardUserHitService;

import com.sykj.util.IDUtils;
import com.sykj.util.RabbitKeys;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;

@Component
@RabbitListener(queues = RabbitKeys.QUEUE_HIT)
public class PrizeHitReceiver {
    private final static Logger logger = LoggerFactory.getLogger(PrizeHitReceiver.class);
    @Autowired
    BisBusinessOrderService bisBusinessOrderService;
    @Autowired
    private CardUserHitService hitMapper;
    @Autowired
    BisNurseBagService bisNurseBagService;
    @Autowired
    BisBagRecordService bisBagRecordService;
//
    @RabbitHandler
    public void processMessage2(byte[] message) {
        logger.info("Receiver1 ===========> : " + new String(message));
    }

    @RabbitHandler
    public void processMessage3(Map message) {
        logger.info("user hit : " + message);
        CardUserHit hit = new CardUserHit();
        // 将中奖后的信息添加到用户中奖纪录表中
        hit.setGameid(MapUtils.getIntValue(message,"gameid"));
        hit.setUserid(MapUtils.getIntValue(message,"userid")+"");
        hit.setProductid(MapUtils.getIntValue(message,"productid")+"");
        hit.setHittime(new Date(MapUtils.getLongValue(message,"hittime")));

        boolean save2 = hitMapper.save(hit);

    }
    @RabbitHandler
    public void processMessage2(CardUserHit message) {

        logger.info("user hit : game={},user={},product={},time={}" , message.getGameid(),message.getUserid(),message.getProductid(),message.getHittime());

        boolean save2 = hitMapper.save(message);
        // 为了使中奖的礼包可以使用，要将数据在添加到 订单表中和 购买礼包记录表中
        //在加入信息之前先查出中奖的礼包信息
        BisNurseBag byId = bisNurseBagService.getById(message.getProductid());
        //添加至营业订单表中
        BisBusinessOrder bisBusinessOrder=new BisBusinessOrder();
        bisBusinessOrder.setOrderId(IDUtils.createID());
        bisBusinessOrder.setOrderContent("活动抽奖中的礼包");
        bisBusinessOrder.setProjectId(message.getProductid());
        bisBusinessOrder.setCustomUserId(message.getUserid());
        bisBusinessOrder.setOrderType("2");
        bisBusinessOrder.setOrderMoney(0);
        // 3 代表 是红包抽奖活动所得；
        bisBusinessOrder.setPayWay("3");
        bisBusinessOrder.setBuyNum(1);
        Timestamp timeStamp = new Timestamp(new Date().getTime());
        bisBusinessOrder.setPayTime(timeStamp);
        bisBusinessOrder.setOrderStatus("3");
        bisBusinessOrder.setCreateTime(timeStamp);
        bisBusinessOrder.setOrderSource("1");
        // 默认商家id
        bisBusinessOrder.setShopId("778516368");
        //向数据库添加
        boolean save = bisBusinessOrderService.save(bisBusinessOrder);

        // 在添加购买礼包记录信息
        BisBagRecord bisBagRecord=new BisBagRecord();
        bisBagRecord.setRecordId(IDUtils.createID());
        bisBagRecord.setBagId(message.getProductid());
        bisBagRecord.setValidDay(byId.getOverdueTime());
        bisBagRecord.setCustomUserId(message.getUserid());
        bisBagRecord.setBuyNum(1);
        bisBagRecord.setOrderId(bisBusinessOrder.getOrderId());
        bisBagRecord.setShareUserId(message.getUserid());
        bisBagRecord.setStatus("1");
        bisBagRecord.setReceiveStatus("1");
        // 添加数据库
        boolean save1 = bisBagRecordService.save(bisBagRecord);
        if(save==true&&save1==true&&save2==true){
            logger.info("sava mysql  status : true");
            System.err.println("sava mysql  status : true");
        }else {
            System.err.println("sava mysql  status : false");
            logger.info("sava mysql  status : false");
        }


    }
}